import store from '../store/index.js'


const request = function(Url, Method, Data, isForm) {

	return new Promise((resolve, reject) => {
		// 展示加载动画
		// uni.showLoading({
		//     title: '努力加载中'
		// })
		let senData = {
			...Data,
		}
		uni.request({
			url: store.state.baseUrl + Url,
			data: senData,
			method: Method,
			header: {
				'token': uni.getStorageSync('token') ? uni.getStorageSync('token') : '',
				'content-type': isForm == 1 ? 'application/x-www-form-urlencoded' :
					"application/json",
			},
			success(response) {
				let res = response.data;
				if (res.code == 401) {
					uni.navigateTo({
						url: '/pages/login/index'
					})
				} else if (res.code == 0 || res.code == 200) {
					resolve(res)
				} else if (res.code == 402) {
					uni.showToast({
						icon: 'none',
						title: res.msg
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/mine/realname'
						})
					}, 1000)
				} else if (res.code == 403) {
					uni.showToast({
						icon: 'none',
						title: res.msg
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/mine/tradeAcount'
						})
					}, 1000)


				} else {
					uni.showToast({
						icon: 'none',
						title: res.msg
					})
				}
			},
			fail(err) {
				uni.showToast({
					title: '网络异常',
					icon: 'none'
				})
				reject(err)
			},
			complete() {
				uni.hideLoading()
			}
		})
	})
}


export default request